<template>
  <div>
    <div class="div-input">
      <el-form :inline="true" style="margin-top: 20px;" :rules="rulesForm" :model="form" ref="form">
        <el-form-item prop="date" label="日期：">
          <el-date-picker type="date" placeholder="选择日期" style="width: 100%;" v-model="form.date" ></el-date-picker>
        </el-form-item>
        <el-form-item label="车次：" style="margin-left: 100px;" prop="trainNumberName">
          <el-input placeholder="请输入车次" v-model="form.trainNumberName">
            <i slot="prefix" class="el-input__icon el-icon-search"></i>
          </el-input>
        </el-form-item>
        <el-form-item style="margin-left: 100px;">
          <el-button type="primary" @click="setOnQuery('form')" :loading="loading">{{ loading ? '查询中...' : '查询' }}</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table :data="tableData" height=337 style="width: 80%; margin-left:100px; margin-bottom: 10px;" stripe
      :header-cell-style="{ background:'#f0f9ff', color: '#000000', textAlign: 'center' }"
      :cell-style="{ textAlign: 'center' }">
      <el-table-column prop="stationNumber" label="站序" width="80">
      </el-table-column>
      <el-table-column prop="stationName" label="车站名">
      </el-table-column>
      <el-table-column prop="trainNumberName" label="车次">
      </el-table-column>
      <el-table-column prop="arriveTime" label="到达时间">
      </el-table-column>
      <el-table-column prop="startTime" label="发车时间">
      </el-table-column>
      <el-table-column label="停留时间">
        <template slot-scope="scope">
          {{ scope.row.stopTime }} 分钟
        </template>
      </el-table-column>
    </el-table>

    <div class="div-footer">
      <span>总共有： {{ total }} 站</span>
      <span v-if="tableData != ''" style="color: #818cf8;">{{ startStation }}-{{ arriveStation
      }}</span>
      <span v-if="tableData != ''" style="color: #818cf8;">耗时：{{ time[0] }}时{{ time[1] }}分</span>
    </div>
  </div>
</template>

<script>
import { dateSub } from '../../../composables/data'

export default {
  name: "trainNumber",
  data() {
    return {
      // 按钮预加载
      loading: false,
      startStation: '',
      arriveStation: '',
      total: 0,
      time: {},
      listData: [],
      form: {
        trainNumberName: "G77",
        date: ""
      },
      rulesForm: {
        date: [
          { type: 'date', required: true, message: '请选择时间', trigger: 'blur' },
        ],
        trainNumberName: [
          { required: true, message: '请选择车次号 ', trigger: 'blur' }
        ]
      },

    };
  },
  methods: {
    getData() {
      this.total = this.listData.length
      this.startStation = this.listData[0].stationName
      this.arriveStation = this.listData[this.total - 1].stationName
      this.time = dateSub(this.listData[0].startTime, this.listData[this.total - 1].arriveTime)
    },

    init() {
      this.listData = [];
      this.total = 0;
    },
    setOnQuery(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.init();
          setTimeout(() => {
            this.onQuery()
          }, 1000)
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    onQuery() {
      console.log(this.form);
      this.request.get('trainstation/getByNumber/' + this.form.trainNumberName + "/" + this.form.date).then(
        response => {
          if (response.data.code === 1) {
            this.listData = response.data.data
            this.getData()
            this.$message({
              message: '查询成功',
              type: 'success',
              duration: 1000
            });
          } else {
            this.$message({
              message: '查询出错: ' + response.data.msg,
              type: 'error',
              duration: 1000
            })
          }
        },
        error => {
          console.log(error.message)
        }
      )
        .finally(() => {
          this.loading = false
        })
    }
  },
  computed: {
    tableData() {
      return this.listData
    }
  }
}
</script>

<style scoped>
.div-input {
  width: 1000px;
  height: 90px;
  background-color: #EEF1F8;
  text-align: center;
  border: 1px solid #5DA8D9;
  margin-bottom: 30px;
  margin-top: 40px;
}

.div-footer {
  margin-left: 100px;
  font-size: larger;
  width: 780px;
  display: flex;
  justify-content: space-around;
}
</style>